package com.kt.service;

import java.util.*;

import com.kt.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kt.dao.UserDao;
import com.kt.po.User;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private UserDao userDao;//jap的dao层
    @Autowired
    private UserMapper userMapper;//mybatis的dao层

    /**
     * jdbcTemplate
     *
     * @return
     */
    //开启事务，出错会进行回滚
    @Transactional
    public int addUser(User user) {
        return jdbcTemplate.update("INSERT INTO `userinfo`( `username`, `password`, `gxqm`, `date`) VALUES (?,?,?,?);",
                user.getUsername(), user.getPassword(), user.getGxqm(), user.getDate().toString());
    }

    public int deleteUserByUsername(String username) {
        return jdbcTemplate.update("DELETE FROM `userinfo` WHERE username=?;", username);
    }

    public List<Map<String, Object>> findAllUser() {
        //前端时间格式转换
        //function changetime(str){
        //return new Date(+new Date(str)+8*3600*1000).toISOString().replace(/T/g,' ').replace(/\.[\d]{3}Z/,'');
        //}
        return jdbcTemplate.queryForList("select * from `userinfo`;");
    }

    /**
     * jpa
     *
     * @return
     */
    public User addUserJpa(User user) {
        return userDao.save(user);
    }

    public User updateUserJpa(User user) {
        return userDao.save(user);
    }

    public void deleteUserJpa(User user) {
        userDao.delete(user);
    }

    public void deleteUserByIdJpa(int id) {
        userDao.deleteById(id);
    }

    public Optional<User> findUserById(int id) {
        return userDao.findById(id);
    }

    public List<User> findAllUserJpa() {
        return userDao.findAll();
    }

    public List<User> findUserByUsernameJpa(String username) {
        return userDao.findUserByUsername(username);
    }

    /**
     * mybatis
     *
     * @return
     */
    public List<User> findUserByNameMap(String username) {
        return userMapper.findUserByNameMap(username);
    }

    public int addUserMap(User user) {
        userMapper.addUserMap(user);
        return user.getId();
    }

    public int deleteUserMap(int id) {
        return userMapper.deleteUserMap(id);
    }

    /**
     * 分页查询
     */
    public List<User> findUserPageMap(int page, int size) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", (page - 1) * size);
        map.put("size", size);
        return userMapper.findUserPageMap(map);
    }

    /**
     * 动态sql多条件查询
     */
    public Map<String, Object> selectUserWithParam(Map<String, Object> map) {
        List<User> users = new ArrayList();
        Map<String, Object> resultMap = new HashMap<>();
        try {
            resultMap.put("status", true);
            resultMap.put("msg", "查询成功");
            users = userMapper.selectUserWithParam(map);
        } catch (Exception e) {
            resultMap.put("status", false);
            resultMap.put("msg", "查询失败");
        }
        resultMap.put("users", users);
        return resultMap;
    }

}
